/*
 * @Date: 2022-03-31 09:57:08
 * @LastEditors: LiuPeng
 * @LastEditTime: 2022-04-11 16:52:09
 * @FilePath: \pay\src\components\Info\AgencyPaySet\index.jsx
 * @Description: 代付配置 新增按钮弹出层所需表单
 * 
 */

import React from 'react'
import { message } from 'antd'


import {
  Row, Col, Form,
  Input, Button
} from 'antd'

//引入样式
import myStyle from './index.module.css'
import { useForm } from 'antd/lib/form/Form';
import {
  modifyMerchantPayment,
  addMerchantPayment,
} from './../../../API'

export default function AgencyPaySet(props) {

  console.log(props);
  // 添加代付配置
  let [addPaymentAgent] = useForm()

  // 输入框验证规则
  const rules = [{ required: true, message: '必填项不能为空' }];

  // 点击编辑按钮传入的一行数据
  let { selecRowData, btntext, selecLeftRowData, setshowmodal } = props
  // 默认 显示value
  let defaultValue = {};
  // 根据 selecRowData 是否存在 
  if (selecRowData) {
    defaultValue = Object.assign(selecRowData)
  }


  // console.log(selecRowData);
  // 提交按钮点击事件
  const SubmitBtn = () => {
    let {
      maxAmountOfMoney,
      minAmountOfMoney,
      rate,
      serviceCharge
    } = addPaymentAgent.getFieldsValue()
    if (btntext === '编辑') {
      // console.log('编辑');
      // 修改商户代付配置数据
      console.log(selecRowData.key);
      modifyMerchantPayment({
        id: selecRowData.key,
        tiXianFeiLv: rate,
        tiXianMax: maxAmountOfMoney,
        tiXianMin: minAmountOfMoney,
        tiXianShouXu: serviceCharge
      }).then(res => {
        console.log(res);
        if (res.code === 200) {
          message.success(res.msg)
          setshowmodal('false')
        } else {
          message.error(res.msg)
          setshowmodal('false')
        }


      })
    } else if (btntext === '增加') {
      // console.log('增加');
      addMerchantPayment({
        tiXianFeiLv: rate.toString(),
        tiXianMax: maxAmountOfMoney.toString(),
        tiXianMin: minAmountOfMoney.toString(),
        tiXianShouXu: serviceCharge.toString(),
        userId: selecLeftRowData.key.toString()
      }).then(res => {
        if (res.code === 200) {
          message.success(res.msg)
          setshowmodal('false')
        } else {
          message.error(res.msg)
          setshowmodal('false')
        }
      })
    }
  }

  return (
    <div>
      <Form
        name="infoShowForm"
        size='large'
        form={addPaymentAgent}
        labelCol={{ span: 24 }}
        labelAlign='left'
        initialValues={defaultValue}
      >
        <Row gutter={16}>
          <Col span={12} className={myStyle.Col_space}>
            <Form.Item
              label='代付金额(最小)'
              rules={rules}
              labelAlign='right'
              name='minAmountOfMoney'

            >
              <Input placeholder='请输入代付最小金额' />
            </Form.Item>
          </Col>
          <Col span={12} className={myStyle.Col_space}>
            <Form.Item
              label='代付金额(最大)'
              rules={rules}
              labelAlign='right'
              name='maxAmountOfMoney'
            >
              <Input placeholder='请输入代付最大金额' />
            </Form.Item>
          </Col>
          <Col span={12} className={myStyle.Col_space}>
            <Form.Item
              label='手续费(元)'
              rules={rules}
              labelAlign='right'
              name='serviceCharge'
            >
              <Input placeholder='请输入代付手续费(元/笔)' />
            </Form.Item>
          </Col>
          <Col span={12} className={myStyle.Col_space}>
            <Form.Item
              label='费率(%)'
              labelAlign='right'
              rules={rules}
              name='rate'
            >
              <Input placeholder='请输入代付费率(%/笔)' />
            </Form.Item>
          </Col>
        </Row>
        <Row>
          <Col span={24} style={{ textAlign: 'center' }}>
            <Form.Item>
              <Button type='primary' onClick={SubmitBtn}>提交</Button>
            </Form.Item>
          </Col>
        </Row>
      </Form>
    </div>
  )
}
